package org.richin.support.orm;

import java.util.Collections;
import java.util.List;

import org.apache.commons.lang.StringUtils;

public class Page<T> {
	public static final String ASC = "asc";
	public static final String DESC = "desc";
	public static final int MIN_PAGESIZE = 5;
	public static final int MAX_PAGESIZE = 200;
	protected int pageNo = 1;
	protected int pageSize = 5;
	protected String orderBy = null;
	protected String order = null;
	protected boolean autoCount = true;
	protected List<T> result = Collections.emptyList();
	protected int totalCount = -1;

	public Page() {
	}

	public Page(int pageSize) {
		setPageSize(pageSize);
	}

	public Page(int pageSize, boolean autoCount) {
		setPageSize(pageSize);
		this.autoCount = autoCount;
	}

	public int getPageNo() {
		return this.pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;

		if (pageNo < 1)
			this.pageNo = 1;
	}

	public int getPageSize() {
		return this.pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;

		if (pageSize < 5)
			this.pageSize = 5;

		if (pageSize > 200)
			this.pageSize = 200;
	}

	public int getFirst() {
		return ((this.pageNo - 1) * this.pageSize);
	}

	public String getOrderBy() {
		return this.orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public boolean isOrderBySetted() {
		return StringUtils.isNotBlank(this.orderBy);
	}

	public String getOrder() {
		return this.order;
	}

	public void setOrder(String order) {
		if(StringUtils.isBlank(order))
			return;
		String[] orders = StringUtils.split(StringUtils.lowerCase(order), ',');
		String[] arr$ = orders;
		int len$ = arr$.length;
		for (int i$ = 0; i$ < len$; ++i$) {
			String orderStr = arr$[i$];
			if ((!(StringUtils.equals("desc", orderStr))) && (!(StringUtils.equals("asc", orderStr))))
				throw new IllegalArgumentException("排序方向" + orderStr + "不是合法值");
		}

		this.order = StringUtils.lowerCase(order);
	}

	public boolean isAutoCount() {
		return this.autoCount;
	}

	public Page<T> setAutoCount(boolean autoCount) {
		this.autoCount = autoCount;
		return this;
	}

	public List<T> getResult() {
		return this.result;
	}

	public void setResult(List<T> result) {
		this.result = result;
	}

	public int getTotalCount() {
		return this.totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getTotalPages() {
		if (this.totalCount < 0)
			return -1;

		int count = this.totalCount / this.pageSize;
		if (this.totalCount % this.pageSize > 0)
			++count;

		return count;
	}

	public boolean isHasNext() {
		return (this.pageNo + 1 <= getTotalPages());
	}

	public int getNextPage() {
		if (isHasNext())
			return (this.pageNo + 1);

		return this.pageNo;
	}

	public boolean isHasPre() {
		return (this.pageNo - 1 >= 1);
	}

	public int getPrePage() {
		if (isHasPre())
			return (this.pageNo - 1);

		return this.pageNo;
	}
}